package com.tecsup.log.servlet;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;


import com.tecsup.log.dao.DetalleCompraMaterialesDAO;
import excepcion.DAOExcepcion;
import com.tecsup.log.dao.CompraMaterialesDAO;
import com.tecsup.log.modelo.DetalleCompraMateriales;
import com.tecsup.log.modelo.CompraMateriales;
/**
 * Servlet implementation class MaterialesInsertarServlet
 */
@WebServlet("/DetalleCompraMaterialesInsertarServlet")
public class DetalleCompraMaterialesInsertarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DetalleCompraMaterialesInsertarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		DetalleCompraMateriales r = new DetalleCompraMateriales();
		double precio=Double.parseDouble(request.getParameter("precio"));
		double cantidad = Double.parseDouble(request.getParameter("cantidad"));
		int material = Integer.parseInt(request.getParameter("material"));
		int ultid = Integer.parseInt(request.getParameter("id"));
		
		r.setPrecio(precio);
		r.setCantidad(cantidad);
		r.setIdMaterial(material);
		r.setIdCompraMaterial(ultid);
		String id = String.valueOf(ultid);		
		DetalleCompraMaterialesDAO dao = new DetalleCompraMaterialesDAO();
		try {
			dao.insertar(r);
			dao.CalcularTotal(ultid);
			Collection<DetalleCompraMateriales> lista = dao.buscarPorId(id);
			request.setAttribute("detallecompramateriales", lista);
			
			RequestDispatcher rd = request
			.getRequestDispatcher("DetalleCompraMaterialesBuscar.jsp");
			rd.forward(request, response);
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}

	}
}
